package test.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class MySQLclient {

    public static void main(String[] args){

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {

            /*  Register the driver
		The following Class.forName() statement is not necessary if using Java SE 6 (1.6) or later */

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            try {

		/* Get a connection to the database */
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/crush_or_flush?" + "user=root&password=root");

                try {

                    stmt = conn.createStatement();

		    /* Execute the query */
                    rs = stmt.executeQuery("SELECT * FROM msg_message_sender");

                    /* The following 3 lines are for finding out the size of the result set */
                    rs.last();
                    int rowCount = rs.getRow();
                    rs.beforeFirst();

                    System.out.println("Retrieved " + rowCount + " row(s).\n");

                    System.out.println("CityName\n--------");

                    /* Retrieve the data from the result set */
                    while (rs.next()) {
                        String CityName = rs.getString("sender_id");
                        System.out.println(CityName);
                    }

                } finally {
                    /* Release the resources */
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException sqlEx) {
                               System.out.println("SQLException: " + sqlEx.getMessage());
                        }
                        rs = null;
                    }

                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (SQLException sqlEx) {
                               System.out.println("SQLException: " + sqlEx.getMessage());
                        }

                        stmt = null;
                    }

                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (SQLException sqlEx) {
                               // Ignore
                        }

                        conn = null;
                    }

                }

            } catch (SQLException ex) {
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            }

        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
        }

    } // main()

} // MySQLclient
